$(document).ready(function () {
    $('#vcodeButton').unbind("click");
    $('#vcodeButton').click(function () {
        var email = $('#email').val();

        if (email == null || email == '') {
            alert("邮箱不能为空");
            return;
        }
        if(email.indexOf("@")<=0){
            alert("邮箱格式错误");
            return;
        }

        $.ajax({
            type: "POST",
            url: "regist/validate",
            dataType: "json",
            data: {
                'email': email
            },
            timeout: 10000,
            async: true,
            success: function (res) {
                if (res.result == 'WRONG') {
                    alert('该邮箱已被使用，请直接登录');
                }
                if (res.result == 'SUCCESS') {
                    alert('验证码已发送，请查收邮件');
                }
                if (res.result == 'FAILED') {
                    alert('发送失败，请重试');
                }

            }

        });
    });
    $('#registButton').unbind("click");
    $('#registButton').click(function () {
        var email = $('#email').val();
        var vcode = $('#vcode').val();
        var username = $('#user').val();
        var password = $('#password').val();
        var password1 = $('#password1').val();


        if (email == null || email == '') {
            alert("邮箱不能为空");
            return;
        }
        if(email.indexOf("@")<=0){
            alert("邮箱格式错误");
            return;
        }
        if (vcode == null || vcode == '') {
            alert("请先获取并填写验证码");
            return;
        }
        if (username == null || username == '') {
            alert("用户名不能为空");
            return;
        }
        if (password == null || password == '') {
            alert("密码不能为空");
            return;
        }
        if(password1!=password){
            alert("两次输入的密码不一致，请确认");
            return;
        }

        $.ajax({
            type: "POST",
            url: "regist",
            dataType: "json",
            data: {
                'email': email,
                'vcode': vcode,
                'username': username,
                'password': password
            },
            timeout: 10000,
            async: false,
            success: function (res) {
                if (res.result == 'SUCCESS') {
                    alert('注册成功，请登录');
                    window.location.href = "login"
                }
                if (res.result == 'FAILED') {
                    alert(res.reason);
                }

            }

        });
    });
});